package com.lechi.iot.client;

import com.lechi.iot.netty.channel.ITcpChannel;
import com.lechi.iot.netty.utils.NettyCacheUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * @author yg
 * @description 设备自检
 */
@Slf4j
public class COMMAND {

    public static final DateTimeFormatter yyyyMMddHHmmss = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    public static final String RESPONSE_END = "\r\n";

    public static final String END = ",ED";


    public static final String QIXIANGJU_IP = "124.114.232.106";
//    public static final String QIXIANGJU_IP = "222.90.6.54";
    public static String getHeartMsg(String stationType) {
        StringBuilder response = new StringBuilder();
        response.append("$LOGIN,")
                .append(stationType)
                .append(",")
                .append("N01")
                .append(",")
                .append("2")
                .append(",")
                .append(LocalDateTime.now().format(yyyyMMddHHmmss))
                .append(COMMAND.RESPONSE_END);
        String result = response.toString();
        return result;
    }

    public static void send(String message) {
        ITcpChannel channel = (ITcpChannel) NettyCacheUtils.get(QIXIANGJU_IP);
        if (Objects.isNull(channel)) {
            return;
        }
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        buffer.writeCharSequence(message, Charset.defaultCharset());
        channel.send(buffer);
//        log.error("成功发送指令报文到西安市气象局报文：{}", message);
    }
}